package cn.edu.hit.core.message;

import cn.edu.hit.core.Document;
import cn.edu.hit.core.Pair;
import cn.edu.hit.core.db.DBReadHelper;
import cn.edu.hit.core.db.DataConnection;
import cn.edu.hit.core.db.DataConnectionLoader;
import cn.edu.hit.core.db.DataReader;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.security.cert.CollectionCertStoreParameters;
import java.util.*;
import java.util.stream.Collectors;

public final class FileAdapter extends MessageAdapterHelper2<Map> {
    @Override
    protected Object getValue(Map map, String id, Document configure) {

        String joinSystemId = configure.getString(DATA_SOURCE_ARGS);
        Map<String, Map<String, Map<Long, Double>>> dataMap = map;
//        String machineNumber = machineId2Number.get(configure.getString("name"));
        Map<Long, Double> longDoubleMap = dataMap.get("030100101").get(joinSystemId);
        if (longDoubleMap == null || longDoubleMap.size() == 0){
            return Collections.EMPTY_LIST;
        }

        return longDoubleMap.entrySet().stream().sorted((e1,e2) -> (int)(e1.getKey()-e2.getKey())).map(e -> e.getValue()).collect(Collectors.toList());
    }

    @Override
    protected Map pretreat(Long timestamp) {
        List<String> jsIds = configureMap.entrySet().parallelStream().map(entry -> {
            Document configure = (Document) entry.getValue();
            return configure.getString(DATA_SOURCE_ARGS);
        }).collect(Collectors.toList());

        Map<String, Map<String, Map<Long, Double>>> mapMap = new HashMap<>();

        if (jsIds.size() == 0)
            return new JSONObject();
        try {
            Map<String, Map<Long, Double>> resultMap = requestAsJson(jsIds, timestamp);
            mapMap.put("030100101",resultMap);
            return mapMap;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JSONObject();
    }

    @Override
    public boolean accept(String type) {
        return "file".equalsIgnoreCase(type);
    }

    private Map requestAsJson(List<String> jsIds, Long timestamp) throws Exception {
        Map<String, Map<Long, Double>> valueMap = new HashMap<>();
        File file = new File("./data.json");
        String str = "";
        if (file.exists()) {
            InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
            BufferedReader br = new BufferedReader(isr);
            String data = null;
            while ((data = br.readLine()) != null) {
                str += data;
            }
            br.close();
        }
        JSONObject jsonObject = JSONObject.fromObject(str);
        jsonObject.keySet().forEach((jsids) -> {
            valueMap.put((String) jsids, new HashMap<>());
            JSONObject timevalue = jsonObject.getJSONObject((String) jsids);
            timevalue.keySet().forEach(timeString -> {
                try {
                    valueMap.get((String) jsids).put(Long.valueOf((String) timeString), timevalue.getDouble((String) timeString));
                } catch (Exception e){

                }

            });
        });
        return valueMap;
    }
}
